import 'package:sow_app_core/sow_app_core.dart';
import '../usecase/usecase.dart';
import '../usecase/user_usecase.dart';

abstract class InfoController {
  final InfoPresenter _presenter;
  final UserRepository _userRepository;

  InfoController(this._presenter, this._userRepository);
  init() => InitUserInfoUseCase(_presenter, _userRepository)(NoParams());

  void changePassword(String oldPass, String newPass) => ChangePasswordUseCase(
      _presenter, _userRepository)([oldPass, newPass].join(' '));
}

class ChangePasswordUseCase extends UseCase<void, String> {
  final InfoPresenter _presenter;
  final UserRepository _userRepository;

  ChangePasswordUseCase(this._presenter, this._userRepository);

  @override
  void call(String params) async {
    try {
      await _userRepository.changePassword(
          params.split(' ')[0], params.split(' ')[1]);
      _presenter.onComplete();
      _presenter.onPop();
    } on ServerException catch (e) {
      _presenter.onError(e);
    }
  }
}
